<script setup lang="ts">
import { onMounted, ref } from 'vue'

// 图书列表数据
const books = ref([
  {
    id: 1,
    title: '三体',
    author: '刘慈欣',
    cover: '/static/images/books/santi.jpg',
    status: 'available',
    createTime: '2025-03-15 10:00:00',
  },
  {
    id: 2,
    title: '活着',
    author: '余华',
    cover: '/static/images/books/huozhe.jpg',
    status: 'borrowed',
    createTime: '2025-03-14 15:30:00',
  },
])

// 加载状态
const loading = ref(false)
const hasMore = ref(true)

// 获取状态文本
function getStatusText(status: string) {
  const statusMap: Record<string, string> = {
    available: '可借阅',
    borrowed: '已借出',
    reserved: '已预约',
  }
  return statusMap[status] || status
}

// 格式化时间
function formatTime(time: string) {
  const date = new Date(time)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

// 跳转到图书详情
function goToDetail(id: number) {
  uni.navigateTo({
    url: `/pages/book/detail/index?id=${id}`,
  })
}

// 跳转到发布页面
function goToPublish() {
  uni.navigateTo({
    url: '/pages/book/publish/index',
  })
}

// 加载更多数据
function loadMore() {
  if (loading.value || !hasMore.value) {
    return
  }

  loading.value = true

  // 模拟加载更多数据
  setTimeout(() => {
    loading.value = false
    hasMore.value = false
  }, 1000)
}

// 页面加载
onMounted(() => {
  // 模拟初始数据加载
  loading.value = true
  setTimeout(() => {
    loading.value = false
  }, 1000)
})
</script>

<template>
  <view class="container">
    <!-- 图书列表 -->
    <view v-if="books.length > 0" class="book-list">
      <view v-for="book in books" :key="book.id" class="book-item" @click="goToDetail(book.id)">
        <image class="cover" :src="book.cover" mode="aspectFill" />
        <view class="info">
          <view class="title">
            {{ book.title }}
          </view>
          <view class="author">
            {{ book.author }}
          </view>
          <view class="status" :class="book.status">
            {{ getStatusText(book.status) }}
          </view>
          <view class="time">
            发布时间：{{ formatTime(book.createTime) }}
          </view>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view v-else class="empty">
      <image class="empty-icon" src="/static/images/empty.svg" mode="aspectFit" />
      <view class="empty-text">
        暂无发布的图书
      </view>
      <view class="publish-btn" @click="goToPublish">
        去发布
      </view>
    </view>

    <!-- 加载更多 -->
    <view v-if="books.length > 0" class="load-more">
      <text v-if="loading">
        加载中...
      </text>
      <text v-else-if="!hasMore">
        没有更多了
      </text>
    </view>
  </view>
</template>

<style lang="scss">
.container {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 30rpx;
}

.book-list {
  .book-item {
    display: flex;
    background: #fff;
    border-radius: 12rpx;
    padding: 20rpx;
    margin-bottom: 20rpx;

    .cover {
      width: 160rpx;
      height: 220rpx;
      border-radius: 8rpx;
      margin-right: 20rpx;
    }

    .info {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .title {
        font-size: 32rpx;
        color: #333;
        font-weight: bold;
        margin-bottom: 10rpx;
      }

      .author {
        font-size: 28rpx;
        color: #666;
        margin-bottom: 10rpx;
      }

      .status {
        display: inline-block;
        padding: 4rpx 16rpx;
        border-radius: 4rpx;
        font-size: 24rpx;
        margin-bottom: 10rpx;

        &.available {
          background: #e8f5e9;
          color: #4caf50;
        }

        &.borrowed {
          background: #fff3e0;
          color: #ff9800;
        }

        &.reserved {
          background: #e3f2fd;
          color: #2196f3;
        }
      }

      .time {
        font-size: 24rpx;
        color: #999;
      }
    }
  }
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 100rpx 0;

  .empty-icon {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 30rpx;
  }

  .empty-text {
    font-size: 28rpx;
    color: #999;
    margin-bottom: 40rpx;
  }

  .publish-btn {
    width: 240rpx;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    background: #4caf50;
    border-radius: 40rpx;
    font-size: 28rpx;
    color: #fff;
  }
}

.load-more {
  text-align: center;
  padding: 30rpx 0;

  text {
    font-size: 24rpx;
    color: #999;
  }
}
</style>
